<template>
  <a-modal
    :open="open"
    width="640px"
    title="填写加工信息"
    destroy-on-close
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="加工方式" class="w-full" v-bind="validateInfos.processMethodName">
        <a-select
          v-model:value="formState.processMethodName"
          :virtual="false"
          :options="processTypeOptions"
          mode="multiple"
          placeholder="请选择"
          @change="onChange"
        />
      </a-form-item>
      <a-form-item label="加工费:" class="w-1/2" v-bind="validateInfos.processMethodCost">
        <a-input v-model:value="formState.processMethodCost" placeholder="请输入">
          <template #prefix> <span class="text-gray-400">￥</span> </template>
        </a-input>
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.processMethodCmemo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const processTypeOptions = ref(null)

const onChange = (val, options = []) => {
  formState.value.sfxmBh = options.map((item) => item.sfxmBh).join(',')
  formState.value.processMethodId = options[0]?.code
  formState.value.processMethodCost = processTypeOptions.value
    .filter((v) => val.some((id) => id === v.value))
    .reduce((prev, cur) => {
      return prev + cur.processMethodCost || 0
    }, 0)
}

const getProjectList = async () => {
  const res = await post('/operation/opex/getProcessMethodInfoList', {
    processMethodName: '',
    status: 1
  })
  if (res.code !== 0) return message.error(res.msg)
  processTypeOptions.value = res.data.map((item) => {
    return {
      label: `${item.processMethodName}${item.yxzt === 1 ? '（关联项目已停用）' : ''} `,
      value: item.processMethodName,
      code: item.id,
      sfxmBh: item.sfxmBh,
      disabled: item.yxzt === 1,
      processMethodCost: item.dj
    }
  })
}

const formState = ref({
  processMethodName: undefined,
  processMethodCost: '',
  processMethodCmemo: ''
})
const rulesState = ref({
  processMethodName: [{ required: true, message: '请选择加工方式' }],
  processMethodCost: [{ required: true, message: '请输入加工费' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  await validate()
  emit('ok', { ...formState.value, processMethodName: formState.value.processMethodName.join(',') })
  onCancel()
}
watch(
  () => props.open,
  (val) => {
    if (val && Object.keys(props.rowData).length) {
      formState.value = { ...props.rowData, processMethodName: props.rowData.processMethodName?.split(',') }
    }
  },
  {
    immediate: true
  }
)
onMounted(() => {
  getProjectList()
})

const onCancel = () => {
  resetFields()
  emit('update:open', false)
}
</script>
